# 运行命令是：docker build --add-host=github.com:20.205.243.166 -t baohuojiangde .
# 抽风的GFW导致Github时常连接不上，只能这么指定Github的ip，


# 使用基础镜像
# 使用 Ubuntu 作为基础镜像
FROM ubuntu:22.04

# 设置时区环境变量（避免交互式安装问题）
ENV DEBIAN_FRONTEND=noninteractive
ENV TZ=Etc/UTC

# 设置 Rust 的镜像源
ENV RUSTUP_DIST_SERVER=https://mirrors.ustc.edu.cn/rust-static
ENV RUSTUP_UPDATE_ROOT=https://mirrors.ustc.edu.cn/rust-static/rustup
#ENV RUSTUP_DIST_SERVER="https://rsproxy.cn"
#ENV RUSTUP_UPDATE_ROOT="https://rsproxy.cn/rustup"
#ENV CARGO_UNSTABLE_SPARSE_REGISTRY=true
ENV LLVM_DIR=/usr/lib/llvm-14

ENV LD_LIBRARY_PATH=/root/.rustup/toolchains/nightly-2021-11-22-x86_64-unknown-linux-gnu/lib:$LD_LIBRARY_PATH

ENV LIBRARY_PATH=/root/.rustup/toolchains/nightly-2021-11-22-x86_64-unknown-linux-gnu/lib:$LIBRARY_PATH

# 将 Rust 添加到环境变量
ENV PATH="/root/.cargo/bin:${PATH}"

# 设置工作目录
WORKDIR /app

# 复制项目文件
COPY ./sources.list /etc/apt/sources.list

COPY ./Tool/safer-c2rust/ /app/safer-c2rust

# 更新系统并安装基础工具和 Python
RUN apt update && apt upgrade -y &&  apt install -y --no-install-recommends \
    python3 python3-pip python3-venv \
    build-essential llvm llvm-dev clang git libclang-dev cmake bear libssl-dev pkg-config git curl zlib1g-dev \
    libpng-dev libevent-dev yacc \
    && apt clean \
    && rm -rf /var/lib/apt/lists/* \
    && mkdir $HOME/.cargo && curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y \
    && echo apt update and rust install successfully \
    && chmod -R a+w $HOME/.cargo \
    && rustup toolchain install nightly-2021-11-22 \
    && echo rust-nightly-2021-11-22 successfully 

RUN mkdir -p /root/.cargo && \
           echo "[source.crates-io]" > /root/.cargo/config.toml && \
           echo "registry = \"https://github.com/rust-lang/crates.io-index\"" >> /root/.cargo/config.toml && \
           echo "replace-with = 'rsproxy'" >> /root/.cargo/config.toml && \
           echo "" >> /root/.cargo/config.toml && \
	   echo "[source.rsproxy]" >> /root/.cargo/config.toml && \
	   echo "registry = \"https://rsproxy.cn/crates.io-index\"" >> /root/.cargo/config.toml && \
	   echo "" >> /root/.cargo/config.toml && \
           echo "[source.rsproxy-sparse]" >> /root/.cargo/config.toml && \
           echo "registry = \"sparse+https://rsproxy.cn/index/\"" >> /root/.cargo/config.toml && \
           echo "" >> /root/.cargo/config.toml && \
           echo "[source.ustc]" >> /root/.cargo/config.toml && \
           echo "registry = \"git://mirrors.ustc.edu.cn/crates.io-index\"" >> /root/.cargo/config.toml && \
           echo "" >> /root/.cargo/config.toml && \
           echo "[source.sjtu]" >> /root/.cargo/config.toml && \
           echo "registry = \"https://mirrors.sjtug.sjtu.edu.cn/git/crates.io-index\"" >> /root/.cargo/config.toml && \
           echo "" >> /root/.cargo/config.toml && \
           echo "[source.rustcc]" >> /root/.cargo/config.toml && \
           echo "registry = \"https://code.aliyun.com/rustcc/crates.io-index.git\"" >> /root/.cargo/config.toml && \
           echo "[net]" >> /root/.cargo/config.toml && \
           echo "git-fetch-with-cli = true" >> /root/.cargo/config.toml \
    && echo cargo/config.toml proxy changed successfully \
    && rustc --version && cargo --version \
    && rustup default 1.70.0\
    && echo rust v1.70.0 install successfully \
    && rm -f rust-toolchain.toml  \
    && cargo install c2rust --force --locked \
    && echo Thanks God, c2rust install successfully 

RUN ln -sf /usr/bin/python3 /usr/bin/python \
    && cd safer-c2rust \
    && pip install --no-cache-dir -i https://pypi.tuna.tsinghua.edu.cn/simple  -r requirements.txt \
#    && echo "20.205.243.166 github.com" >> /etc/hosts \
    && python build.py -a \
    && cd /root \
    && curl -O http://ftp.gnu.org/gnu/m4/m4-1.4.9.tar.gz \
    && tar -zvxf m4-1.4.9.tar.gz \
    && cd m4-1.4.9 \
    && ./configure \
    && make \
    && make install \
    && cd ../ \
    && curl -O ftp://ftp.gnu.org/gnu/autoconf/autoconf-2.69.tar.gz \
    && tar zxvf autoconf-2.69.tar.gz \
    && cd autoconf-2.69 \
    && ./configure \
    && make \
    && make install \
    && cd ../ \
    && curl -O https://ftp.gnu.org/gnu/automake/automake-1.16.5.tar.gz \
    && tar -xzvf automake-1.16.5.tar.gz \
    && cd automake-1.16.5 \
    && ./configure \
    && make \
    && make install


# 安装 Rust
# RUN mkdir $HOME/.cargo && curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y \
#    && chmod -R a+w $HOME/.cargo

# RUN rustup toolchain install nightly-2021-11-22

# RUN echo $LD_LIBRARY_PATH
# RUN ls /root/.rustup/toolchains/

# 创建 .cargo 目录并配置代理
#RUN mkdir -p /root/.cargo && \
#    echo "[source.crates-io]" > /root/.cargo/config.toml && \
#    echo "registry = \"https://github.com/rust-lang/crates.io-index\"" >> /root/.cargo/config.toml && \
#    echo "replace-with = 'ustc'" >> /root/.cargo/config.toml && \
#    echo "" >> /root/.cargo/config.toml && \
#    echo "[source.tuna]" >> /root/.cargo/config.toml && \
#    echo "registry = \"https://mirrors.tuna.tsinghua.edu.cn/git/crates.io-index.git\"" >> /root/.cargo/config.toml && \
#    echo "" >> /root/.cargo/config.toml && \
#    echo "[source.ustc]" >> /root/.cargo/config.toml && \
#    echo "registry = \"git://mirrors.ustc.edu.cn/crates.io-index\"" >> /root/.cargo/config.toml && \
#    echo "" >> /root/.cargo/config.toml && \
#    echo "[source.sjtu]" >> /root/.cargo/config.toml && \
#    echo "registry = \"https://mirrors.sjtug.sjtu.edu.cn/git/crates.io-index\"" >> /root/.cargo/config.toml && \
#    echo "" >> /root/.cargo/config.toml && \
#    echo "[source.rustcc]" >> /root/.cargo/config.toml && \
#    echo "registry = \"https://code.aliyun.com/rustcc/crates.io-index.git\"" >> /root/.cargo/config.toml && \
#    echo "[http]" >> /root/.cargo/config.toml && \
#    echo "check-revoke = false" >> /root/.cargo/config.toml

# 验证安装
#RUN rustc --version && cargo --version
#
#RUN rustup default 1.70.0
#
#RUN cargo install c2rust --locked

# 安装依赖
# RUN ln -sf /usr/bin/python3 /usr/bin/python && pip install --no-cache-dir -i https://pypi.tuna.tsinghua.edu.cn/simple  -r safer-c2rust/requirements.txt

# # 暴露应用运行的端口（假设你的项目运行在 5000 端口）
# EXPOSE 5000

# # 定义容器启动命令
# CMD ["python", "app.py"]

# 安装c2rust的各种环境
# RUN cd safer-c2rust && python build.py -a

# 给shell脚本添加可执行权限
#RUN chmod +x ./setup_and_build.sh

# 运行shell脚本docker p
# RUN ./setup_and_build.sh

# 测试运行一个example下面的项目
#CMD [ "python", "run.py", "c2rust" , "--local_path",  "examples/urlparser/url.h", "safer", "stat" ]

COPY ./Tool/others /app/

ENTRYPOINT ["/bin/bash", "run.sh"]
